package sctu.edu.java.year2016.team02.huangguojing.day20160605.Chapter12_1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

/**
 * Created by Eoly on 2016/6/5.
 */
public class ProgressRead extends JFrame implements ActionListener {
    private File file;
    private InputStream in;
    private ProgressMonitorInputStream read;
    private JLabel Path;
    private Button open;
    private JFileChooser openFile;

    public ProgressRead(String title){
        super(title);
        open = new Button("Open");
        open.setBounds(50,35,75,25);
        open.addActionListener(this);
        openFile = new JFileChooser("d:/dev");
        Path = new JLabel("");
        Path.setBounds(50,10,200,25);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setSize(300, 100);
        add(open);add(Path);
        setResizable(false);
        setLayout(null);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e){
            openFile.showOpenDialog(this);
            if(openFile.getSelectedFile()!=null){
                file = openFile.getSelectedFile();
                Path.setText(file.getAbsolutePath());
                try {
                    in = new FileInputStream(file);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                read = new ProgressMonitorInputStream(this,"Reading~~~",in);
                ProgressMonitor pm = read.getProgressMonitor();
                byte[] b=new byte[1024];
                try {
                    read.read(b,0,b.length);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                System.out.println(new String(b,0,b.length));
                try {
                    read.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
    }
}
